<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
    <link rel="stylesheet" type="text/css" href="css/mainstyle.css" />
    <link rel="stylesheet" type="text/css" href="css/radiocheck.css" />
    <link rel="stylesheet" type="text/css" href="css/button.css" />
    <link rel="icon" href="image/logo.ico" type="image/x-icon">
    <title>疫苗预约</title>
    <style type="text/css">
        .clearfix:after {
        content: ".";
        display: block;
        height: 0;
        clear: both;
        visibility: hidden;
        }
        #main_body_part{
            width: 90%; 
            margin-left: 3%;
            float: left;
            margin-top: 2rem;
        }
        #main_body_add{
            padding-top: 0.5rem;
            width: 12%;
            height: 10%;
            font-size: 1.3rem;
            float: left;
            
        }
        #main_body_name_input,#main_body_sex_input,#main_body_id_input{
            float: left;
            width: 50%;
            height: 10%;
           
        }
        #main_body_submit_button{
            margin: auto;
        }
        #add_name,#add_id{
            width: 100%;
            height: 2rem;
        }
        #type_input1{
            float: left;
            margin-top: 0.8rem;
            margin-right: 1rem;
        }
        #main_head_imgb{
            background-color: #ececee;
            border-radius: 50%;
        }
        .un,.un:link,.un:visited,.un:hover,.un:active{
        text-decoration: none;
        color:inherit;
        }
        .dialog { position: fixed; left: 50%; top: 50%; background-color: rgba(0,0,0,0.7);
                z-index: 9999; padding: 5px 10px; color: #fff; border-radius: 5px;
                transform: translate(-50%, -50%); -webkit-transform: translate(-50%, -50%);
                -moz-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%);
                -o-transform: translate(-50%, -50%); }
    </style>
    <?php
        function errreturn(string $err){//信息重复时
            echo "<script>parent.location.href='addfamily.php';alert('".$err."');</script>";
        }
        function yes2(string $err){//信息插入成功时返回家人页面
            echo "<script>parent.location.href='main_family.php';alert('".$err."');</script>";
        }
        include_once 'api/DBcon.php';
        $result=false;
        $link=OpenDB();
        @session_start();
        $userName=$_SESSION['userName'];//用户名
        $headImgSrc=$_SESSION['imgsrc'];//头像路径

        if(isset($_POST['submit'])&&$link){
            $name=$_POST['add_name'];
            $sex=$_POST['sex'];
            $id=$_POST['add_id'];  
            $idUser=$_SESSION['idUser'];
            $todate=getdate();
            $age=$todate['year']-substr($id,6,4);

            $query="select * from information where id=\"${id}\";";//查重，保证唯一性
            $result = mysqli_query($link,$query);
            if(mysqli_num_rows($result)) { 
            errreturn("成员信息已存在");
            }
            else{//确定成员不存在后，将信息插入数据库
                $query="INSERT INTO information values('${id}','${name}','${sex}','${age}','no','${idUser}','image/ma.png',null,null,null,null);";
                $result = mysqli_query($link,$query);
                if($result){ //成功插入回复注册成功信息              
                    yes2("添加成功！");
                }
                else{//插入失败处理
                    errreturn("出错了！添加失败");
                }            
            }
        }
            
    ?>
    <script>
        function dialog(d,time){       
           if(document.getElementById("dialog")){        
            document.getElementById("dialog").innerHTML = d;        
            document.getElementById("dialog").style.display = "block";        
           }
           else{       
            var dialog  = document.createElement("div");       
            dialog.id = "dialog";        
            dialog.className = "dialog";        
            dialog.innerHTML = d;        
            document.getElementsByTagName("body")[0].appendChild(dialog);        
           };
        
           setTimeout(function(){        
            var dialog = document.getElementById("dialog");       
                if(dialog){       
                    document.getElementById("dialog").style.display = "none";        
                }
            }, time || 2000);       
        } 
        
        function check(){
            
            var sex = document.getElementsByName("sex");
            var userid = document.getElementById("add_id");
            var name = document.getElementById("add_name");      
            var tid=trim(userid.value);//身份证号输入内容
            var tname=trim(name.value);//名字输入内容
            
            var untest = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;//验证身份证号码的正则表达
            if(tname==null || tname=="") {
                dialog("请输入姓名哦");
                name.focus();
                return false;
            } 
            if(!(sex[0].checked)&&!(sex[1].checked)){
                dialog("请选择性别哦");
                return false;
            }
            if(tid==null || tid==""||!(untest.test(tid))){
                dialog("请输入正确的身份证号码哦");
                userid.focus();
                return false;
            }
            return true;
            }
            function trim(str){ //删除左右两端的空格
   　　         return str.replace(/(^\s*)|(\s*$)/g, "");
            }
        </script>
</head>
<body>
    <div id="left_part">
    <div id="navigationBar">
        <div id=logo>
            <img src="image/logo1.png" title="疫苗预约" id="logoimg"/>
            <img src="image/text.png" title="yuyue" id="logotext"/>
        </div>
        <div id="buttons">
            <a href="main_family.php">
            <div class="button" id="family" onmouseover="this.style.background='#84858d14'" onmouseout="this.style.background=''">
                <img src="image/jiaren.png" title="我的家人" id="icon"/>
                我的家人
            </div>
            </a>
            <a href="main_reservation.php">
            <div class="button" id="reservation" onmouseover="this.style.background='#84858d14'" onmouseout="this.style.background=''">
                <img src="image/yuyue.png" title="预约注射" id="icon"/>
                预约注射
            </div>
            </a>
            
            <a href="main_notice.php">
            <div class="button" id="notice" onmouseover="this.style.background='#84858d14'" onmouseout="this.style.background=''">
                <img src="image/zhuyi.png" title="注意事项" id="icon"/>
                注意事项
            </div>
            </a>
        </div>   
    </div>
    <div id="user">
        <img src="<?php echo $headImgSrc;?>" title="头像" id="headimg"/>
        <div id="userId"><?php echo $userName;?></div>
        <a href="updateuser.php"><img src="image/dangan.png" title="注意事项" id="myId"/></a>
        
    </div>
    <a href="index.php"><div id="bottom">退出登录</div></a>
</div>
    <div id="main">
        
        <div id="main_head">
            <div id="main_head_text">添加成员></div>
            <div id="main_head_imgb"><img src="image/wenhua.jpeg" title="添加" id="main_head_icon"/></div>
        </div>
        
        <div id="main_body">
        <form action="" method="post" id="form1">
            <div id="main_body_part" class="clearfix">
                <div id="main_body_add">姓名：</div>
                <div id="main_body_name_input">
                    <input type="text" name="add_name" id="add_name" autocomplete="off"/>
                </div>
            </div>

            <div id="main_body_part">
                <div id="main_body_add">性别：</div>
                <div id="main_body_sex_input">
                    <div id="type_input1"><input type="radio" name="sex" class="gcs-radio" id="sex1" value="男"/>
                    <label for="sex1"></label><label for="sex1" >男</label></div>
                    <div id="type_input1"><input type="radio" name="sex" class="gcs-radio" id="sex2" value="女"/>
                    <label for="sex2"></label><label for="sex2">女</label></div>
                </div>
            </div>

            <div id="main_body_part">
                <div id="main_body_add">身份证号：</div>
                <div id="main_body_id_input">
                    <input type="text" name="add_id" id="add_id" autocomplete="off"/>
                </div>
            </div>

            <div id="main_body_part">
                    <div id="main_body_submit_button">
                        <button type="submit" name="submit" id="submit" value="resubmit" class="buttons green">添加到家庭成员</button>
                    </div>
            </div>
            <script>
                var subm2 =document.getElementById("submit");
                var form1 =document.getElementById("form1");
                subm2.onclick=function(){
                    var result=check();
                    if(result)
                    form1.onsubmit=function(){return true};
                    else
                    form1.onsubmit=function(){return false};
                }
            </script>
        </form>
        </div>
        <div id="main_bottom">
            <span>地址：武汉市东湖新技术开发区文华园路8号</span>
            <span>作者：文华学院信息学部计科1806刘昊天</span>
        </div>
    </div>
    
</body>
</html>